package zl.applet.oldsnakey;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Collections;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.KeyStroke;

public class OldSnakey extends JFrame implements ActionListener
{
	private static final long serialVersionUID = 1L;

	public static void main(String[] args)
	{
		new OldSnakey();
	}

	private JMenuBar jmb;    //声明菜单栏
	private JMenu jmStart;   //声明“开始”菜单
	private JMenu jmHelp;    //声明“帮助”菜单
	private JMenuItem jmiStart;  //开始/暂停 菜单栏
	private JRadioButton jrbSpeedMin;   //最小速度
	private JRadioButton jrbSpeedMed;   //中等速度
	private JRadioButton jrbSpeedMax;   //最快速度
	private JMenuItem jmiStop;   //停止 菜单栏
	private JMenuItem jmiShowCharts;  // 显示排行榜 菜单栏
	private JMenuItem jmiClearCharts;  //清空排行榜 菜单栏
	private JMenuItem jmiQuit;  //退出 菜单栏
	private JMenuItem jmiF1;   //声明 帮助 菜单栏
	private JMenuItem jmiAbout;  //声明 关于 菜单栏
	private MyPanel mp;    //贪吃蛇面板
	private int speed = 400;    //初始速度
	private JPanel topPanel;   
	private JLabel score;  //显示分数的label
	private Charts charts = new Charts();   //排行榜对象

	//构造、实例化
	public OldSnakey()
	{
		this.jmiStart = new JMenuItem("开始/暂停");
		this.jmiStart.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE,0));  //添加快速访问键
		this.jmiStart.addActionListener(this);      //添加事件监听器
		this.jrbSpeedMin = new JRadioButton("低速");
		this.jrbSpeedMed = new JRadioButton("中速");
		this.jrbSpeedMax = new JRadioButton("高速");
		this.jmiStop = new JMenuItem("停止");
		this.jmiStop.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0));
		this.jmiStop.addActionListener(this);
		ButtonGroup bg = new ButtonGroup();   //按钮组、用来装单选按钮
		this.jrbSpeedMin.setSelected(true);  //默认选中
		bg.add(this.jrbSpeedMin);
		bg.add(this.jrbSpeedMed);
		bg.add(this.jrbSpeedMax);
		this.jmiShowCharts = new JMenuItem("查看排行榜");
		this.jmiShowCharts.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,0));
		this.jmiShowCharts.addActionListener(this);
		this.jmiClearCharts = new JMenuItem("清空排行榜");
		this.jmiClearCharts.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,0));
		this.jmiClearCharts.addActionListener(this);
		this.jmiQuit = new JMenuItem("退出");
		this.jmiQuit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE,0));
		this.jmiQuit.addActionListener(this);
		this.jmiF1 = new JMenuItem("操作说明");
		this.jmiF1.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1,0));
		this.jmiF1.addActionListener(this);
		this.jmiAbout = new JMenuItem("关于(A)");
		this.jmiAbout.setMnemonic('A');
		this.jmiAbout.addActionListener(this);

		this.jmStart = new JMenu("开始(S)"); 
		this.jmStart.setMnemonic('S');
		this.jmStart.add(this.jmiStart);
		this.jmStart.add(this.jrbSpeedMin);
		this.jmStart.add(this.jrbSpeedMed);
		this.jmStart.add(this.jrbSpeedMax);
		this.jmStart.add(this.jmiStop);
		this.jmStart.add(this.jmiShowCharts);
		this.jmStart.add(this.jmiClearCharts);
		this.jmStart.add(this.jmiQuit);
		this.jmHelp = new JMenu("帮助(H)");
		this.jmHelp.setMnemonic('H');
		this.jmHelp.add(this.jmiF1);
		this.jmHelp.add(this.jmiAbout);

		this.jmb = new JMenuBar();
		this.jmb.add(this.jmStart);
		this.jmb.add(this.jmHelp);

		this.setJMenuBar(this.jmb);

		this.topPanel = new JPanel();
		this.score = new JLabel("分数：0");
		this.topPanel.add(this.score);
		this.add(this.topPanel,BorderLayout.NORTH);

		this.setTitle("贪吃蛇游戏");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //设置窗体的默认关闭操作
		this.mp = new MyPanel(this);
		this.mp.setFocusable(true);
		this.add(mp);
		this.setSize(316,481);   //设置窗体的默认大小
		this.setResizable(false);
		this.setLocation(    //设置屏幕的居中显示
			this.getToolkit().getScreenSize().width / 2 - this.getWidth() /2,
			this.getToolkit().getScreenSize().height / 2 - this.getHeight() /2
		);
		this.addWindowListener(new WindowAdapter(){   //添加窗体关闭、打开事件监听器
			public void windowOpened(WindowEvent e)
			{
				try
				{
					charts.load();
				}
				catch (Exception ex)
				{
					ex.printStackTrace();
				}		
			}

			public void windowClosing(WindowEvent e)
			{
				try
				{
					charts.save();
				}
				catch (Exception ex)
				{
					ex.printStackTrace();
				}
			}
		});

		this.setVisible(true);
	}

	//设置单选按钮的可用性
	public void setRadioButton(boolean bool)
	{
		this.jrbSpeedMin.setEnabled(bool);
		this.jrbSpeedMed.setEnabled(bool);
		this.jrbSpeedMax.setEnabled(bool);
	}

	public void setScore(int score)
	{
		//设置显示分数
		this.score.setText("分数：" + score);
	}

	//停止游戏后执行
	public void showResult(int score)
	{
		String name = JOptionPane.showInputDialog(this,"请输入姓名：","消息",JOptionPane.INFORMATION_MESSAGE);
		if(name.length() == 0)
			name = null;
		this.charts.add(new Player(name,score));
		Collections.sort(this.charts.getAllPlayer());
		this.showCharts();
	}
	
	//显示排行榜
	public void showCharts()
	{
		StringBuffer sb = new StringBuffer();
		if(this.charts.getAllPlayer().size() == 0)
			sb.append("暂无玩家记录！");
		else
		{
			for(Player p : this.charts.getAllPlayer())
			{
				sb.append(p.getName() + "       -->       " + p.getScore() + "\n");
			}
		}
		JOptionPane.showMessageDialog(this,sb.toString(),"消息",JOptionPane.INFORMATION_MESSAGE);
	}

	//响应事件
	public void actionPerformed(ActionEvent e)
	{
		Object obj = e.getSource();
		if(obj == this.jmiStart)   //如果单击的是开始/暂停
		{		
			this.setRadioButton(false);   //一旦开始游戏、则单选按钮不可用
			if(this.mp.getState() == SnakeState.first|| this.mp.getState() == SnakeState.stop)
			{
				if(this.jrbSpeedMin.isSelected())
					this.speed = 400;
				else if(this.jrbSpeedMed.isSelected())
					this.speed = 300;
				else if(this.jrbSpeedMax.isSelected())
					this.speed = 200;
				this.mp.setState(SnakeState.start);
				this.mp.start(this.speed);   //改变蛇的状态
			}
			else if(this.mp.getState() == SnakeState.start) //
				this.mp.setState(SnakeState.pause);   //如果是开始。则暂停
			else if(this.mp.getState() == SnakeState.pause)
				this.mp.setState(SnakeState.start);  //如果是暂停则开始
			//else if(this.mp.getState() == SnakeState.pause || this.mp.getState() == SnakeState.stop)
			//	this.mp.setState(SnakeState.start);
		}
		else if(obj == this.jmiShowCharts)  //查看排行榜
		{
			this.showCharts();
		}
		else if(obj == this.jmiClearCharts)   //清空排行榜
		{
			this.charts.getAllPlayer().clear();
		}
		else if(obj == this.jmiQuit)   //停止
		{
			System.exit(0);
		}
		else if(obj == this.jmiStop)  //停止
		{
			this.mp.setState(SnakeState.stop,"已停止游戏");
			this.setRadioButton(true);
		}
		else if(obj == this.jmiF1)  //帮助
		{
			JOptionPane.showMessageDialog(this,"利用键盘的上(↑)下(↓)左(←)右(→)" +
			"\n改变贪吃蛇移动方向不断吃到食物、" + 
				"\n碰到墙壁和自身则失败！" + 
				"\nSPACE键---暂停\\开始" + 
				"\nENTER键---停止" + "\nESC键---退出游戏","消息",JOptionPane.INFORMATION_MESSAGE);
		}
		else if(obj == this.jmiAbout)  //关于
		{
			JOptionPane.showMessageDialog(this,"贪吃蛇小游戏\n版本：1.0\n作者：张乐\nEmail:1603013767@qq.com","消息",JOptionPane.INFORMATION_MESSAGE);
		}
	}
}